// pages/tradingArea/manageHotGoods.js
const controller = require("../controllers/Controller").controller

Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageNumber: 1,
    shopId: "",
    lastPage: false,
    name: "",
    list: [''],
    index: 0,
    failDesc: ""
  },
  openReason(e) {
    let id = e.currentTarget.dataset.id
    this.data.list.forEach(item => {
      if (item.id == id) {
        this.setData({
          failDesc: item.failDesc
        })
        wx.showModal({
          title: '理由',
          content: item.failDesc,
          showCancel: false,
          success(res) {
            if (res.confirm) {
              console.log('用户点击确定')
            }
          }
        })
      }
    })
  },

  //跳转核销记录
  toWriteOffHistory(e) {
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/tradingArea/writeOffHistory?id=' + id + "&type=2",
    })
  },

  // 跳转修改页
  toUpdatePage(e) {
    let id = e.currentTarget.dataset.id
    let type = e.currentTarget.dataset.type
    wx.navigateTo({
      url: '/pages/tradingArea/addCoupon?id=' + id + "&type=" + type,
    })
  },

  getShopActivityPage() {
    let value = {}
    value.pageNumber = this.data.pageNumber
    value.shopId = this.data.shopId
    value.type = this.data.type
    value.name = this.data.name
    controller.getshopBagPage(value).then(res => {
      let list = res.data.list
      list.forEach(item => {
        item.startTime = item.startTime.split(" ")[0]
        item.endTime = item.endTime.split(" ")[0]
        item.effectiveStartTime = item.effectiveStartTime.split(" ")[0]
        item.effectiveEndTime = item.effectiveEndTime.split(" ")[0]
      })
      this.setData({
        list: list,
        lastPage: res.data.lastPage
      })
    })
  },


  //跳转优惠券详情
  toDetail(e) {
    let index = e.currentTarget.dataset.index
    let type = e.currentTarget.dataset.type
    let id = this.data.list[index].id
    wx.navigateTo({
      url: '/pages/tradingArea/couponDetail?type=' + type + '&id=' + id,
    })
  },

  //跳转用户明细
  toStatDetail(e) {
    let index = e.currentTarget.dataset.index
    let couponDetail = this.data.list[index]
    console.log(couponDetail)
    wx.navigateTo({
      url: '/pages/tradingArea/userDetail?type=1' + '&couponDetail=' + encodeURIComponent(JSON.stringify(couponDetail)),
    })
  },
  back() {
    wx.navigateBack({
      delta: 0,
    })
  },

  //获取搜索名称
  getSearchText(e) {
    this.setData({
      name: e.detail.value
    })
  },

  //删除
  del(e) {
    let value = {}
    value.couponBagId = e.currentTarget.dataset.id
    value.shopId = e.currentTarget.dataset.shopid
    controller.delShopDelBag(value).then(res => {
      wx.showToast({
        icon: "none",
        title: '删除成功',
        duration: 1500
      })
      this.setData({
        pageNumber: 1
      })
      this.getShopActivityPage()
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      shopId: wx.getStorageSync('shopId'),
      type: options.type
    })
    this.getShopActivityPage()

  },

  onReachBottom: function () {
    this.getMore()
  },


  getMore() {
    if (this.data.lastPage == false) {
      let pageNumber = this.data.pageNumber + 1
      this.setData({
        pageNumber: pageNumber
      })
      let value = {}
      value.pageNumber = this.data.pageNumber
      value.shopId = this.data.shopId
      value.type = this.data.type
      controller.getshopBagPage(value).then(res => {
        let list = this.data.list
        let list2 = res.data.list
        res.data.list2.forEach(item => {
          item.startTime = item.startTime.split(" ")[0]
          item.endTime = item.endTime.split(" ")[0]
          item.effectiveStartTime = item.effectiveStartTime.split(" ")[0]
          item.effectiveEndTime = item.effectiveEndTime.split(" ")[0]
        })
        let list3 = list.concat(list2)
        this.setData({
          list: list3,
          lastPage: res.data.lastPage
        })
      })
    } else {

    }
  }
})